package dao

import (
	"context"
	"fmt"
	"goadmin/conf"

	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)

var mdb *mongo.Database

func init() {
	client, err := mongo.Connect(context.Background(), options.Client().ApplyURI(fmt.Sprintf("mongodb://%s:%d", conf.Mongodb.Host, conf.Mongodb.Port)))
	if err != nil {
		panic(err)
	}
	mdb = client.Database(conf.Mongodb.Db)
	indexModel := mongo.IndexModel{
		Keys: bson.D{
			{Key: "fromId", Value: 1},
			{Key: "toId", Value: 1},
			{Key: "createdAt", Value: 1},
		},
	}
	_, err = mdb.Collection("msg").Indexes().CreateOne(context.Background(), indexModel)
	if err != nil {
		panic(err)
	}
}
